<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="./styles/Setting.css" />
    <link rel="stylesheet" href="./styles/import.css" />
    <link rel="stylesheet" href="./styles/export.css" />
    <link rel="stylesheet" href="./styles/PopUpImage.css" />
    <link rel="stylesheet" href="./styles/Glimmer.css" />
    <link rel="icon" href="https://www.macrohard.fun/assets/favicon.ico" type="image/x-icon" />
    <link rel="shortcut icon" href="https://www.macrohard.fun/assets/favicon.ico" type="image/x-icon" />
    <title>CSOL 集成工具</title>
</head>
<body>
    <button id="ImportSetting" class="import" onclick="document.getElementById('__impl_ImportSetting').click()">导入</button>
    <input id="__impl_ImportSetting" type="file" style="display: none;" />
    <button id="ExportSetting" class="export">导出</button>
</body>

<!-- 依赖 -->
<script src="scripts/luaparse.js"></script>
<script src="scripts/Version.js"></script>
<script src="scripts/Export.js"></script>

<script type="module">
import * as LuaASTUtil from "./scripts/LuaASTUtil.js"
import * as Widget from "./scripts/Widget.js"
let response = await fetch("./Setting.json")
if (!response.ok) {
    throw Error("Failed to fetch `Setting.json`")
}

let text = await response.text()
let json = JSON.parse(text)
let main_widget = Widget.WidgetFactory(json, 1)
document.body.append(main_widget.element)
window.main_widget = main_widget
Widget.SwitchEventPublisher.bootstrap()

document.getElementById("__impl_ImportSetting").addEventListener('change', (event) => {
    const file = event.target.files[0]
    if (!file) {
        throw "指定了非法的文件对象。"
    }
    const reader = new FileReader()
    reader.onload = (e) => {
        const content = e.target.result
        let ast = luaparse.parse(content)
        let setting = Widget.ImportSetting(ast)
        main_widget.collect(setting)
    }
    reader.readAsText(file)
})
document.getElementById("ExportSetting").onclick = async () => {
    const setting = Widget.ExportSetting(main_widget)
    if (!setting) {
        return
    }
    Export("Setting.lua", setting)
}
</script>
</html>